package cn.tedu.tmall.admin.mall.service.impl;

import cn.tedu.tmall.admin.mall.dao.persist.repository.ICategoryRepository;
import cn.tedu.tmall.admin.mall.pojo.entity.Category;
import cn.tedu.tmall.admin.mall.pojo.param.CategoryAddNewParam;
import cn.tedu.tmall.admin.mall.pojo.vo.CategoryListItemVO;
import cn.tedu.tmall.admin.mall.pojo.vo.CategoryStandardVO;
import cn.tedu.tmall.admin.mall.service.ICategoryService;
import cn.tedu.tmall.common.enumerator.ServiceCode;
import cn.tedu.tmall.common.ex.ServiceException;
import cn.tedu.tmall.common.pojo.vo.PageData;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl implements ICategoryService {
    @Value("${tmall.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    private ICategoryRepository iCategoryRepository;
    @Override
    public void addNew(CategoryAddNewParam categoryAddNewParam) {
        //TODO 1.类别必须唯一
        int count = iCategoryRepository.countByName(categoryAddNewParam.getName());
        if(count>0){
            String message = "类别加载失败，该类别名已被占用啦~~~";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        //TODO 2.如果parentId不为零，则查询数据库，确认是存在的；

        Integer depth = 1;
        Long parentId = categoryAddNewParam.getParentId();
        CategoryStandardVO parentCategory = null;
        if(parentId!=0){
            parentCategory = iCategoryRepository.getStandardById(parentId);
            if (parentCategory == null) {
                String message = "父级类别不存在";
                throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
            }
            depth = parentCategory.getDepth()+1;
        }

        //TODO 3.补全属性值：depth,isParent;
        //TODO 关于depth：如果parent_id为0，depth为1，否则为父级的depth+1；
        //TODO 关于isParent：如果本次添加的类别为某个类别的子级，且父级的isParent为0，则需要将父级的isParent改为1；

        Category category = new Category();
        BeanUtils.copyProperties(categoryAddNewParam,category);
        category.setDepth(depth);
        category.setIsParent(0);
        int rows = iCategoryRepository.insert(category);
        if(rows!=1){
            String message = "服务器繁忙，请稍后重试";
            throw new ServiceException(ServiceCode.ERROR_INSERT,message);
        }

        if (parentCategory != null && parentCategory.getIsParent()==0) {
            Category updateCategory = new Category();
            updateCategory.setId(parentId);
            updateCategory.setIsParent(1);
            rows = iCategoryRepository.updateById(updateCategory);
            if(rows!=1){
                String message = "服务器繁忙，请稍后重试";
                throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
            }

        }

    }

    @Override
    public void deleteById(Long id) {
        //TODO 1.该类别是否存在
        //TODO 2.是否为父级
        //TODO 3.该类别如果为父级的最后一个子级，最需要该父级的isParent为0；
        int row = iCategoryRepository.deleteById(id);
        if(row!=1){
            String message = "删除类别失败";
            throw new ServiceException(ServiceCode.ERROR_DELETE,message);
        }

    }

    @Override
    public void setEnable(Long id) {
        updateEnable(id,ENABLE_STATE_ON);
    }

    @Override
    public void setDisable(Long id) {
        updateEnable(id,ENABLE_STATE_OFF);
    }

    @Override
    public void setDisplay(Long id) {
        updateDisplay(id,DISPLAY_STATE_ON);
    }

    @Override
    public void setHide(Long id) {
        updateDisplay(id,DISPLAY_STATE_OFF);
    }

    @Override
    public PageData<CategoryListItemVO> listByParentId(Long parentId, Integer pageNum) {
        PageData<CategoryListItemVO> pageData =
                iCategoryRepository.listByParentId(parentId, pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<CategoryListItemVO> listByParentId(Long parentId, Integer pageNum, Integer pageSize) {
        PageData<CategoryListItemVO> pageData =
                iCategoryRepository.listByParentId(parentId, pageNum, pageSize);
        return pageData;
    }

    private void updateEnable(Long id,Integer enable){
        //TODO 1.该类别是否存在
        //TODO 2.参数enable与该该类别中的enable是否相等
        CategoryStandardVO standardById = iCategoryRepository.getStandardById(id);
        if (standardById == null) {
            String message = "该类别不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        if(standardById.getEnable().equals(enable)){
            String message = ENABLE_STATE_TEXT[enable]+"类别失败，该类别已"+ENABLE_STATE_TEXT[enable];
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
        Category category = new Category();
        category.setId(id);
        category.setEnable(enable);
        int row = iCategoryRepository.updateById(category);
        if(row!=1){
            String message = ENABLE_STATE_TEXT[enable]+"类别失败,服务器繁忙，请稍后重试";
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
    }

    private void updateDisplay(Long id,Integer display){
        //TODO 1.该类别是否存在
        //TODO 2.参数display与该该类别中的display是否相等
        CategoryStandardVO standardById = iCategoryRepository.getStandardById(id);
        if (standardById == null) {
            String message = "该类别不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        if(standardById.getIsDisplay().equals(display)){
            String message = DISPLAY_STATE_TEXT[display]+"类别失败，该类别已"+DISPLAY_STATE_TEXT[display];
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
        Category category = new Category();
        category.setId(id);
        category.setIsDisplay(display);
        int row = iCategoryRepository.updateById(category);
        if(row!=1){
            String message = DISPLAY_STATE_TEXT[display]+"类别失败,服务器繁忙，请稍后重试";
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
    }
}
